﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OCR
{
    public partial class Search : Form
    {
        //全局结果数据集合
        private List<HistoryInfo> historys;

        private List<SearchResult> searchResults;
        public Search()
        {
            InitializeComponent();
          
        }

        private void btHistory_Click(object sender, EventArgs e)
        {
            History frm = new History();
            frm.ShowDialog();
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            if(this.tbKey.Text.Trim()!="")
            {

          
            //判断有没有全局结果数据
                if (File.Exists(OCR.HistoryFilePath))
                {
                    string str = File.ReadAllText(OCR.HistoryFilePath);
                    historys = JsonConvert.DeserializeObject<List<HistoryInfo>>(str);

                    searchResults = new List<SearchResult>();

                    foreach (HistoryInfo history in historys)
                    {
                        foreach (Card card in history.Results)
                        {
                            if (this.rbAll.Checked)
                            {
                                if (card.NoValue.Contains(this.tbKey.Text.Trim()) || card.PwValue.Contains(this.tbKey.Text.Trim()))
                                {
                                    this.searchResults.Add(CreateSearchResult(history, card));
                                }
                            }
                            else if (this.rbNo.Checked)
                            {
                                if (card.NoValue.Contains(this.tbKey.Text.Trim()))
                                {
                                    this.searchResults.Add(CreateSearchResult(history, card));
                                }
                            }
                            else if (this.rbPw.Checked)
                            {
                                if (card.PwValue.Contains(this.tbKey.Text.Trim()))
                                {
                                    this.searchResults.Add(CreateSearchResult(history, card));
                                }
                            }

                        }
                    }
                    searchResults.Sort(delegate(SearchResult a, SearchResult b) { return b.Date.CompareTo(a.Date); });
                    this.gvResult.DataSource = searchResults;
                    if (this.searchResults.Count == 0)
                    {
                        this.pbImage.Image = null;
                    }
                }
                else
                {

                }
            }
        }
        private SearchResult CreateSearchResult(HistoryInfo history, Card card)
        {
            SearchResult searchResult = new SearchResult();
            searchResult.Date = history.Date;
            searchResult.NoImagePath = Path.Combine(history.ImageFolder, card.NoImageName);
            searchResult.No = card.NoValue;
            searchResult.PwImagePath = Path.Combine(history.ImageFolder, card.PwImageName);
            searchResult.Pw = card.PwValue;
            searchResult.ImagePath = Path.Combine(history.ImageFolder, card.ImageName + ".jpg");
            return searchResult;
        }
        private void gvResult_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string imagePath = this.gvResult.Rows[e.RowIndex].Cells["ImagePath"].Value.ToString();
            this.pbImage.Image = new Bitmap(this.gvResult.Rows[e.RowIndex].Cells["ImagePath"].Value.ToString());
            this.pbNo.Image = new Bitmap(this.gvResult.Rows[e.RowIndex].Cells["NoImagePath"].Value.ToString());
            this.pbPw.Image = new Bitmap(this.gvResult.Rows[e.RowIndex].Cells["PwImagePath"].Value.ToString());
        }

        private void Search_Load(object sender, EventArgs e)
        {
            this.searchResults = new List<SearchResult>();
            this.gvResult.DataSource = this.searchResults ;
        }

    }

    public class SearchResult
    {
        public string Date { get; set; }

        public string NoImagePath { get; set; }
        public string No { get; set; }
        public string PwImagePath { get; set; }
        public string Pw { get; set; }
        public string ImagePath { get; set; }
    }
}
